//
//  HeartRateZoneItem.swift
//  DaRings
//
//  Created by 魔样科技 on 2022/11/2.
//

import Foundation
import MYModule
import UIKit


open class HeartRateZoneItem: StackView {
    
    open override func makeUI() {
        super.makeUI()
        
        self.spacing = 5
        self.axis = .horizontal
        self.alignment = .center
        self.contentMode = .center
        
        self.addArrangedSubview(self.iconView)
        self.iconView.snp.makeConstraints { make in
            make.width.height.equalTo(8)
            
        }
        self.addArrangedSubview(self.titleLabel)
        self.addArrangedSubview(self.dottedLineView)
        self.dottedLineView.snp.makeConstraints { make in
            make.height.equalTo(1)
        }
        
        let hourStack = StackView()
        hourStack.axis = .horizontal
        hourStack.alignment = .lastBaseline
        hourStack.contentMode = .center
        hourStack.addArrangedSubview(hourLabel)
        hourStack.addArrangedSubview(hourUntilLabel)
        
        let minuteStack = StackView()
        minuteStack.axis = .horizontal
        minuteStack.alignment = .lastBaseline
        minuteStack.contentMode = .center
        minuteStack.addArrangedSubview(minuteLabel)
        minuteStack.addArrangedSubview(minuteUntilLabel)
        
        self.addArrangedSubview(hourStack)
        self.addArrangedSubview(minuteStack)

    }
    
    public func updateDate(iconColor: UIColor, title: String = "", hour: Int, minute: Int, second: Int) {
        self.iconView.backgroundColor = iconColor
        self.titleLabel.text = title
        
        self.hourLabel.isHidden = false
        self.hourUntilLabel.isHidden = false
        self.minuteLabel.isHidden = false
        self.minuteUntilLabel.isHidden = false
        
        if hour == 0 && minute == 0 && second == 0 {
            
            self.minuteLabel.text = "--"
            self.hourLabel.text = "--"
            
            return
        }
        
        // 小于一分钟
        if hour == 0 && minute == 0 && second < 60 {
            
            self.hourLabel.isHidden = true
            self.hourUntilLabel.isHidden = true
            
            self.minuteLabel.text = "<1"
            return
        }
        
        self.hourLabel.text = "\(hour)"
        self.minuteLabel.text = "\(minute)"

    }
    
    open override func layoutSubviews() {
        super.layoutSubviews()
        dottedLineView.drawDashLine(strokeColor: UIColor.assist_D0D0D0, direction: .horizontal)
    }
    
    //MARK: - lazy load -
    public lazy var iconView: View = {
        let view = View()
        view.cornerRadius = 4
        return view
    }()
    
    public lazy var titleLabel: Label = {
        let label = Label()
        label.font = Fonts.systemFont(ofSize: 12)
        label.textColor = UIColor.assist_A9A9A9
        label.textAlignment = .center
        return label
    }()
    
    public lazy var dottedLineView: View = {
        let view = View()
        return view
    }()
    
    public lazy var hourLabel: Label = {
        let label = Label()
        label.font = Fonts.boldSystemFont(ofSize: 16)
        label.text = "--"
        label.textColor = UIColor.assist_222222
        label.textAlignment = .center
        label.isHidden = true
        return label
    }()
    
    public lazy var hourUntilLabel: Label = {
        let label = Label()
        label.font = Fonts.systemFont(ofSize: 10)
        label.textColor = UIColor.assist_222222
        label.textAlignment = .center
        label.text = "heart_rate_zone_hour".language
        label.isHidden = true
        return label
    }()
    
    public lazy var minuteLabel: Label = {
        let label = Label()
        label.text = "--"
        label.font = Fonts.boldSystemFont(ofSize: 16)
        label.textColor = UIColor.assist_222222
        label.textAlignment = .center
        label.isHidden = true
        return label
    }()
    
    public lazy var minuteUntilLabel: Label = {
        let label = Label()
        label.font = Fonts.systemFont(ofSize: 10)
        label.textColor = UIColor.assist_222222
        label.textAlignment = .center
        label.text = "heart_rate_zone_min".language
        label.isHidden = true
        return label
    }()
    
}
